package cn.chinaflame.pagodas.service;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.dto.PaOrderitemDto;
import cn.chinaflame.pagodas.entity.PaOrderitem;
import cn.chinaflame.pagodas.enums.CodeEnum;
import cn.chinaflame.pagodas.repository.PaOrderitemRepository;
import cn.chinaflame.pagodas.req.PaOrderitemReq;
import cn.chinaflame.pagodas.utils.ParamPreconditions;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author created by lbq
 * @date 2018年1月5日 下午2:56:18
 **/
@Service
public class PaOrderitemService {
    @Autowired
    private PaOrderitemRepository paOrderRepository;
    @Autowired
    private PaProductPointService paProductPointService;
    @Autowired
    private PaOrderitemRepository paOrderitemRepository;

    /**
     * 查询所有订单
     *
     * @return
     */
    public List<PaOrderitemDto> findAll() {
        List<PaOrderitem> list = paOrderRepository.findAll();
        List<PaOrderitemDto> dtoList = Lists.newArrayList();
        BeanUtils.copyProperties(list, dtoList);
        return dtoList;
    }

    /**
     * 根据id查询单个订单
     *
     * @param id
     * @return
     */
    public PaOrderitemDto findOrderitemByid(String id) {
        PaOrderitem ywOrder = paOrderRepository.findOne(id);
        PaOrderitemDto dto = new PaOrderitemDto();
        BeanUtils.copyProperties(ywOrder, dto);
        return dto;
    }

    /**
     * 根据订单id更新订单表
     *
     * @param id
     * @return
     */
    public PaOrderitemDto orderitemUpdate(String orderId) {
        PaOrderitem ywOrderitem = paOrderRepository.findOne(orderId);
        paOrderRepository.saveAndFlush(ywOrderitem);
        PaOrderitemDto dto = new PaOrderitemDto();
        BeanUtils.copyProperties(ywOrderitem, dto);
        return dto;
    }

    /**
     * 添加订单
     *
     * @param req
     * @return
     */
    public PaOrderitemDto addOrderitem(PaOrderitemReq req) {
        req.check();
        String orderitemId = String.valueOf(System.currentTimeMillis());
        req.setId(orderitemId);
/*		req.setPayStatus("0");
		req.setAddTime(DateTimes.nowDate());
		req.setCreatetime(DateTimes.nowDate());
		req.setUpdatetime(DateTimes.nowDate());*/
        PaOrderitem ywOrderitem = new PaOrderitem();
        BeanUtils.copyProperties(req, ywOrderitem);
        ywOrderitem = paOrderRepository.save(ywOrderitem);
        PaOrderitemDto dto = new PaOrderitemDto();
        BeanUtils.copyProperties(ywOrderitem, dto);
        return dto;
    }
    public Object flash(String orderitemId){
    	if(orderitemId.length() >= 9){
			orderitemId = paOrderitemRepository.findByOrderId(orderitemId).get(0).getId();
		}
		PaOrderitem orderitem = paOrderRepository.findOne(orderitemId);
		ParamPreconditions.checkNotNull(orderitem, CodeEnum.FAIL.getCode(), "未找到订单详情信息");
		paProductPointService.flash(orderitemId, orderitem.getBeWishedName(),orderitem.getBeWishedMemo());
		return Result.create("修改成功");
    }
    /**
     * 修改祝福语
     * @param orderitemId
     * @param beWishedMemo
     * @return
     */
    public Object modifyBewishedMemo(String orderitemId, String beWishedMemo){
    	PaOrderitem item = paOrderRepository.findOne(orderitemId);
		ParamPreconditions.checkNotNull(item, CodeEnum.FAIL.getCode(), "未找到记录");
		ParamPreconditions.checkArgument(!Strings.isNullOrEmpty(beWishedMemo) && beWishedMemo.length() <= 10, CodeEnum.FAIL.getCode(), "祝福语不能为空且不能超过10个汉字");
		item.setBeWishedMemo(beWishedMemo);
		paOrderRepository.saveAndFlush(item);
		paProductPointService.modifyBlessMemo(orderitemId, item.getBeWishedName(), beWishedMemo);
		return Result.create("修改成功");
    }
}










